package uva.asimov.asimovtrainner;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;

import android.database.Cursor;
import android.util.Log;

public class ExportManager {

	private DataBaseManager manager;
	private Cursor cursor;
	private static final String ASIMOV = MainActivity.class.getSimpleName();
	private String[] id;

	public void toXML(File ficheroXML) {
		try {
			
			Log.d(ASIMOV,"----------_>0");			

			DocumentBuilderFactory domParserFactory = DocumentBuilderFactory.newInstance();
			
			Log.d(ASIMOV,"----------_>0.1");
			
			//domParserFactory.setSchema(schema);
			//SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.XML_DTD_NS_URI);
			
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			
			Transformer transformer = transformerFactory.newTransformer();
			
			Log.d(ASIMOV,"----------_>0.12");
			
			//Schema schema = schemaFactory.newSchema(new File("Entrenamientos.dtd"));
				
			Log.d(ASIMOV,"----------_>0.13");
			
			//transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "Entrenamientos.dtd");
			
				//transformer.transform(new StreamSource("Entrenamientos.xml"), new StreamResult(System.out));
			
			
			//domParserFactory.setSchema(schema);
			
			Log.d(ASIMOV,"----------_>0.14");
			
			domParserFactory.setValidating(true);
			
			Log.d(ASIMOV,"----------_>0.2");
			
			domParserFactory.
			
			DocumentBuilder parser = domParserFactory.newDocumentBuilder();
			
			Log.d(ASIMOV,"----------_>0.3");
			
			org.w3c.dom.Document doc = parser.newDocument();
			
			
			org.w3c.dom.Element rootElement = doc.createElement("entrenamientos");
			
			Log.d(ASIMOV,"----------_>0.4");
			
			doc.appendChild(rootElement);
			
			Log.d(ASIMOV,"----------_>1");
			
			// Noticia noticia = new MiNoticia();
			// this.inicia(); System.out.printf("%s\n",this.cantidad());
			for (int i = 0; i < id.length; i++) {
				// noticia = this.elemento();
				cursor = manager.buscarEntrenamiento(id[i]);
				cursor.moveToFirst();

				Log.d(ASIMOV,"----------_>2");
				
				org.w3c.dom.Element eEntrenamiento = doc
						.createElement("entrenamiento");
				rootElement.appendChild(eEntrenamiento);

				Log.d(ASIMOV,"----------_>3");
				
				Attr attTipo = doc.createAttribute("tipo");
				attTipo.setValue(cursor.getString(1));
				eEntrenamiento.setAttributeNode(attTipo);

				Attr attDescripcion = doc.createAttribute("descripcion");
				attDescripcion.setValue(cursor.getString(2));
				eEntrenamiento.setAttributeNode(attDescripcion);

				Attr attFecha = doc.createAttribute("fecha");
				attFecha.setValue(cursor.getString(3));
				eEntrenamiento.setAttributeNode(attFecha);

				Log.d(ASIMOV,"----------_>4");
				
				Attr attDuracion = doc.createAttribute("duracion");
				attDuracion.setValue(cursor.getString(4));
				eEntrenamiento.setAttributeNode(attDuracion);

				Attr attDistancia = doc.createAttribute("distancia");
				attDistancia.setValue(cursor.getString(5));
				eEntrenamiento.setAttributeNode(attDistancia);

				Log.d(ASIMOV,"----------_>5");
				
				Attr attVelocidad = doc.createAttribute("velocidad");
				attVelocidad.setValue(cursor.getString(6));
				eEntrenamiento.setAttributeNode(attVelocidad);

				Attr attOtros = doc.createAttribute("otros");
				attOtros.setValue(cursor.getString(7));
				eEntrenamiento.setAttributeNode(attOtros);

				Attr attUbicacion = doc.createAttribute("ubicacion");
				attUbicacion.setValue(cursor.getString(8));
				eEntrenamiento.setAttributeNode(attUbicacion);

				Log.d(ASIMOV,"----------_>6");
				
//				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				
				Log.d(ASIMOV,"----------_>7");
				
				//Transformer transformer = transformerFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				
				Log.d(ASIMOV,"----------_>8");
				
				StreamResult result = new StreamResult(ficheroXML);
				
				Log.d(ASIMOV,"----------_>9");
				
				transformer.transform(source, result);
				
				Log.d(ASIMOV,"----------_>10");
			}

		} /*catch (Exception e) {
			System.out.printf("Error parsing file");
			Log.d(ASIMOV, "Error parsing file");
		}*/catch (FactoryConfigurationError e1) {
		    // unable to get a document builder factory
			Log.d(ASIMOV, "Error parsing file FactoryConfigurationError");
		} 
		catch (ParserConfigurationException e2) {
			Log.d(ASIMOV, "Error parsing file FactoryConfigurationException");
			// parser was unable to be configured
		}catch (Exception e){
			Log.d(ASIMOV, "Error parsing file ");
		}
	
	}

	public void exportAll(DataBaseManager manager, File ficheroDestino) {

		this.manager = manager;
		Log.d(ASIMOV, "Antes de cursor");
		cursor = manager.cargarCursor();
		cursor.moveToFirst();
		Log.d(ASIMOV, "EN EXPORT ALL "+cursor.getCount()+cursor.getString(0));
		id=new String[cursor.getCount()];
		for(int i=0;i<cursor.getCount();i++){
			id[i]=cursor.getString(0);
			cursor.moveToNext();
		}
		this.toXML(ficheroDestino);
	}

	public void exportSome(DataBaseManager manager, File ficheroDestino,
			String claves[]) {

		this.manager = manager;
		Log.d(ASIMOV, "Antes de cursor");
		cursor = manager.cargarCursor();
		cursor.moveToFirst();
		id=new String[claves.length];
		for (int i = 0; i < claves.length; i++) {
			id[i] = claves[i];
		}
		this.toXML(ficheroDestino);
	}
}
